package com.jwds.digital.bios.business.service.service.belt.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwds.digital.bios.business.service.service.belt.IBeltRouteInfoService;
import com.jwds.digital.bios.business.core.infrastructure.dto.belt.BeltRouteInfoReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.vo.belt.BeltRouteInfoRespVO;
import com.jwds.digital.bios.business.core.infrastructure.dto.belt.assembler.BeltRouteInfoAssembler;
import com.jwds.digital.bios.business.core.infrastructure.entity.belt.BeltRouteInfo;
import com.jwds.digital.bios.business.core.infrastructure.mapper.belt.BeltRouteInfoMapper;
import com.jwds.digital.bios.business.core.exceptions.RecordNotFoundException;
import com.jwds.tdc.framework.application.lib.page.PageRequest;
import com.jwds.tdc.framework.application.lib.page.PageResponse;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 路线信息实现类
 * @author 林柏云
 * @date 2024-10-24
 */
@Slf4j
@Service
public class BeltRouteInfoServiceImpl extends ServiceImpl<BeltRouteInfoMapper, BeltRouteInfo> implements IBeltRouteInfoService {

  @Autowired
  private BeltRouteInfoMapper baseMapper;

  @Override
  public BeltRouteInfoRespVO queryById(Long id) {
    if(id == null) {
      return null;
    }
    BeltRouteInfo entity = baseMapper.selectById(id);
    return BeltRouteInfoAssembler.entity2dto(entity);
  }

  @Override
  public PageResponse<BeltRouteInfoRespVO> page(PageRequest<BeltRouteInfoReqDTO> request) {
    BeltRouteInfoReqDTO condition = request.getQueryCondition();
    Page<BeltRouteInfoRespVO> page = new Page<>(request.getPageIndex(), request.getPageSize());
    IPage<BeltRouteInfoRespVO> pageData = baseMapper.pageQuery(page, condition);
    return new PageResponse<>((Page<?>)pageData, pageData.getRecords());
  }

  @Override
  public List<BeltRouteInfoRespVO> list(BeltRouteInfoReqDTO reqDto) {
    return baseMapper.queryList(reqDto);
  }

  @Override
  public void add(BeltRouteInfoReqDTO reqDto) {
    BeltRouteInfo entity = BeltRouteInfoAssembler.dto2entity(reqDto);
    baseMapper.insert(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void batchAdd(List<BeltRouteInfoReqDTO> dtoList) {
    dtoList.forEach(this::add);
  }

  @Override
  public void update(BeltRouteInfoReqDTO reqDto) {
    BeltRouteInfo oldEntity = baseMapper.selectById(reqDto.getId());
    if (oldEntity == null) {
      throw new RecordNotFoundException();
    }
    BeltRouteInfo newEntity = BeltRouteInfoAssembler.dto2entity(reqDto, oldEntity);
    baseMapper.updateById(newEntity);
  }

  @Override
  public void delete(Long id) {
    BeltRouteInfo oldEntity = baseMapper.selectById(id);
    if (oldEntity == null) {
      throw new RecordNotFoundException();
    }
    baseMapper.deleteById(id);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void batchDelete(List<Long> ids) {
    for (Long id : ids) {
      delete(id);
    }
  }

}